#include "csvtools.h"
#include <QComboBox>
#include <QTableWidget>
#include <QTextCodec>

CSVTools::CSVTools()
{

}

bool CSVTools::tabelSaveFile(QTableWidget *tabel, QString filePath)
{
    if(filePath.isEmpty()){
        return false;
    }

    auto colCount = tabel->model()->columnCount();
    auto rowCount = tabel->model()->rowCount();

    if(!colCount || !rowCount){
        return false;
    }

    QtCSV::StringData csvData;
    QStringList data;

    for(int i = 0 ; i < colCount ; i++){
        data << tabel->model()->headerData(i,Qt::Horizontal).toString();
    }
    csvData << data;



    for(int row = 0 ; row < rowCount ; row++){
        data.clear();
        for(int col = 0 ; col < colCount ; col++){

            auto combox = qobject_cast<QComboBox*>(tabel->cellWidget(row,col));
            if(combox){
                data << combox->currentText();
            }else{
                data << tabel->model()->index(row,col).data().toString();
            }

        }
        csvData << data;
    }

    auto st = write(filePath, csvData);
    if(st){
        system(QString(R"(start %1)").arg(filePath).toLocal8Bit());
    }

    return st;

}

bool CSVTools::tabelSaveFile(QSqlQueryModel *model, QString filePath)
{
    if(filePath.isEmpty()){
        return false;
    }

    auto colCount = model->columnCount();
    auto rowCount = model->rowCount();

    if(!colCount || !rowCount){
        return false;
    }

    QtCSV::StringData csvData;
    QStringList data;

    for(int i = 0 ; i < colCount ; i++){
        data << model->headerData(i,Qt::Horizontal).toString();
    }
    csvData << data;



    for(int row = 0 ; row < rowCount ; row++){
        data.clear();
        for(int col = 0 ; col < colCount ; col++){


            data << model->index(row,col).data().toString();


        }
        csvData << data;
    }

    auto st = write(filePath, csvData);
    if(st){
        system(QString(R"(start %1)").arg(filePath).toLocal8Bit());
    }

    return st;

}

bool CSVTools::tabelLoadFile(QTableWidget *tabel, QString filePath)
{
    if(filePath.isEmpty()){
        return false;
    }

//    auto data =  QtCSV::Reader::readToList(filePath,",","",QTextCodec::codecForName("GBK"));

    auto data = readToList(filePath);

    if(data.isEmpty()){
        return false;
    }

    auto rowCount = data.count() - 1 ;
    auto colCount = data.at(0).count();


    tabel->setRowCount(rowCount);

    for(int row = 0;row < rowCount ; row++){
        for(int col = 0 ; col < colCount ; col++){
            QString info = data[row+1][col];
            auto combox = qobject_cast<QComboBox*>(tabel->cellWidget(row,col));
            if(combox){
                combox->setCurrentText(info);
            }else{
                tabel->setItem(row,col,new QTableWidgetItem(info));
            }
        }
    }

    return true;
}

bool CSVTools::write(const QString &filePath, const QtCSV::AbstractData &data)
{
    return QtCSV::Writer::write(filePath, data,",","",
                                QtCSV::Writer::REWRITE,QStringList(),QStringList(),
                                QTextCodec::codecForName("GBK"));
}

QList<QStringList> CSVTools::readToList(const QString &filePath)
{
    QList<QStringList> rData;
    QFile pFile(filePath);

    if(pFile.open(QIODevice::ReadOnly |QIODevice::Text)){

        QTextCodec *codec = QTextCodec::codecForName("GBK");
        QTextStream in(&pFile);
        while (!in.atEnd()) {
            auto data = pFile.readLine();
            auto line = codec->toUnicode(data.remove(data.count()-1,1)) ;
            rData << line.split(",");
            qDebug() << rData.count(); // 或者你可以在这里处理每一行数据
        }

    }

    pFile.close();
    return rData;

//    QtCSV::Reader::readToData()
//    return QtCSV::Reader::readToList(filePath,",","",QTextCodec::codecForName("GBK"));
}
